<?php

namespace app\controllers;

use yii\db\Query;
use yii\web\Controller;


class ManageController extends AdminBaseController {

    //管理员列表
    public function actionIndex(){
        $list=(new Query())->select('*')->from('yii_admin')->all();
        foreach ($list as $k=>$v){
          $list[$k]['role_id']=$this->role($v['role_id']);
        }
        return $this->render('index',['list'=>$list]);
    }

    //添加管理员
    public function actionAdd(){
        if ($this->post()){
            $data=\Yii::$app->request->post();
            $data['password']=\Yii::$app->getSecurity()->generatePasswordHash($data['password']);
            $data['create_time']=time();
            $data['update_time']=time();
            if($this->check($data['account'])){
                return json_encode(['code'=>0,'msg'=>'管理员已存在']);
            }
            $res=\Yii::$app->db->createCommand()->insert('yii_admin',$data)->execute();
            if ($res){
                return json_encode(['code'=>1,'msg'=>'添加成功']);
            }else{
                return json_encode(['code'=>0,'msg'=>'添加失败']);
            }
        }
        $menu=$this->GetRole();
        return $this->render('add',['menu'=>$menu]);
    }

     //
     public function actionPassword(){
     	if ($this->post()){
     		$data=\Yii::$app->request->post();
     		$id=\Yii::$app->user->identity->id;
     	    $data['password']=\Yii::$app->getSecurity()->generatePasswordHash($data['password']);
            $data['update_time']=time();
            $res=\Yii::$app->db->createCommand()->update('yii_admin',$data,array('id'=>$id))->execute();
            if ($res){
                return json_encode(['code'=>1,'msg'=>'更新成功']);
            }else{
                return json_encode(['code'=>0,'msg'=>'更新失败']);
            }	
     	}
     	
     	
     	
     	
     	   return $this->render('password');
     }

   

    //角色名称
    public function role($role_id){
     $list=(new Query())->select('name')->from('yii_role')->where(array('id'=>$role_id))->one();
     return $list['name'];
    }
    
    
    

    //编辑管理员
    public function actionEdit(){
        if ($this->post()){
            $data=\Yii::$app->request->post();
           // $data['password']=\Yii::$app->getSecurity()->generatePasswordHash($data['password']);
            $data['update_time']=time();
            $res=\Yii::$app->db->createCommand()->update('yii_admin',$data,array('id'=>$data['id']))->execute();
            if ($res){
                return json_encode(['code'=>1,'msg'=>'更新成功']);
            }else{
                return json_encode(['code'=>0,'msg'=>'更新失败']);
            }
        }
        $id=$this->GetId();
        $list=(new Query())->select('*')->from('yii_admin')->where(array('id'=>$id))->one();
        $menu=$this->GetRole();
        return $this->render('edit',['menu'=>$menu,'list'=>$list]);
    }

    //删除管理员
    public function actionDel(){
        $id=$this->PostId();
        $res=\Yii::$app->db->createCommand()->delete('yii_admin',array('id'=>$id))->execute();
        if ($res){
            return json_encode(['code'=>1,'msg'=>'删除成功']);
        }else{
            return json_encode(['code'=>0,'msg'=>'删除失败']);
        }
    }

    //角色
    public function GetRole(){
     $list=(new Query())->select('*')->from('yii_role')->all();
     return $list;
    }

    //检查管理员是否存在
    public function check($account){
        $check=(new Query())->from('yii_admin')->where(array('account'=>$account))->exists();
        if ($check){
            return  true;
        }
    }

    //检查管理员是否存在
    public function checkadmin($role_id){
        $check=(new Query())->from('yii_admin')->where(array('role_id'=>$role_id))->exists();
        if ($check){
            return  true;
        }else{
            return false;
        }
    }


    //意见反馈
    public function actionOpinion(){
        $list=(new Query())->select('*')->from('yii_user_opinion')->all();
        return $this->render('opinion',['list'=>$list]);
    }

    //删除反馈内容
    public function actionDelopinion(){
        $id=$this->PostId();
        $res=\Yii::$app->db->createCommand()->delete('yii_user_opinion',array('id'=>$id))->execute();
        if ($res){
            return json_encode(['code'=>1,'msg'=>'删除成功']);
        }else{
            return json_encode(['code'=>0,'msg'=>'删除失败']);
        }
    }

}
